#!/usr/bin/perl -w
use DBI;
use mydb;
use function;
use Net::SCP::Expect;
use myserver;
use File::Basename qw<basename dirname>;

#进行转码业务的处理功能，接收传入的服务器编号以及准备编号
sub meragescp {

        $tserver = shift||"";
        $pid = shift||0;
        $tid = shift||0;


	print "server:[$tserver] pid = [$pid]";
        if($tserver eq '' || $pid == 0 ) {
                die "参数错误！！！";
	}

        my_connect();

        $sql = "update t_prepare set status ='running', scomment=\'$tserver\' where pid = $pid";

        $sth = $dbconn->prepare($sql) or die $dbconn->errstr;

        $sth->execute() or die "错误:$sth->errstr";

        #把相关的prepare的记录给提取出来，然后生成servertask的记录。
	
        $sql = "select * from  t_prepare where pid = $pid";

        $sth = $dbconn->prepare($sql) or die $dbconn->errstr;

        $sth->execute() or die "错误:$sth->errstr";

	my @dat=$sth->fetchrow_array();
	$pid = $dat[0];
	$tid = $dat[1];
	$pserver = $dat[17];
	$ppath = $dat[18];
	$vcode = $dat[7];
	$acode = $dat[8];

#根据服务器名称取出服务器的IP地址和用户名密码，准备拷入数据文件
	$dip =  get_ServerIP($pserver);
	$path = get_ServerPath($tserver);

	@userpasswd = get_ServerUserPasswd($tserver);
	$ruser = $userpasswd[0];
	$rpasswd = $userpasswd[1];

#设置任务拷贝状态
	setTaskStatus($rid, "copying");

	my $scpe = Net::SCP::Expect->new(user=>"$ruser",password=>"$rpasswd", auto_yes=>1);

  	$scpe->scp("$dip:$ppath", "$path");


#取拷贝文件的文件名和路径。

	$sfile = basename $ppath;	
	$sfile = "$path\/$sfile";
	$playtime = 1;
	$cptime = 0;

#拷贝完成后开始转码
	setTaskStatus($rid, "transcode");
	
	$flag = "error";
#开始转码的任务
	unless (open FH, "-|") {

  		exec "ffmpeg -y -i $sfile  $sfile.mp4 -acodec copy -vcodec copy -f mp4 2>&1|tr '\r' '\n'";        # 用exec执行另一个程序
  		exit;
	}
	while (<FH>) {

          $s_tt = $_;

          $index_found = index($s_tt, "Duration:");

          if($index_found > 0) {

            @a_all = split(/,/, $s_tt);
            @a_a0 = split(/:/, $a_all[0]);
#convert String Date to second;

            $playtime = $a_a0[1]*3600 + $a_a0[2]*60 + $a_a0[3];
	  }

          $index_found = index($s_tt, "time=");

          if($index_found > 0) {

	    $s_tt0 = substr($s_tt, $index_found, length($s_tt)-$index_found);
            @a_a0 = split(/ /, $s_tt0);
            @a_a1 = split(/=/, $a_a0[0]);

	    $ptime = $a_a1[1]*100/$playtime;

	    if($cptime != $ptime) {
		
		$cptime = $ptime;
	    	setTaskProgress($rid, $ptime);
	    }

	  }
#如果判断显示“ global headers"则认为该视频已经完成

	  $index_found = index($s_tt, "global headers");
	  if($index_found >=0) {
	        
		$flag = "success";
		setTaskStatus($rid, "transcode-down");
		setTaskProgress($rid, 100);
	  }

	}

	if($flag eq "error") {

		setTaskStatus($rid, "error", "the video transcode error!");
	}
	
#把所有的状态都置一遍

        $sql = "update c_server set crun= crun-1 WHERE server = \'$tserver\'";
        $sth1 = $dbconn->prepare($sql) or die $dbconn->errstr;
        $sth1->execute() or die "错误: $sth->errstr";

        #设置完成的时间

        $sql = "update c_servertask set etime=now() WHERE rid = $rid";
        $sth1 = $dbconn->prepare($sql) or die $dbconn->errstr;
        $sth1->execute() or die "错误: $sth->errstr";

        #对于任务控制表设定开始的进程数

        $sql = "update c_control set running = running - 1, over = over+1  WHERE tid = $tid";
        $sth1 = $dbconn->prepare($sql) or die $dbconn->errstr;
        $sth1->execute() or die "错误: $sth->errstr";

        #对于任务表状态更改 transcode=>transcode-down

        $sql = "update t_task set status = 'transcode-down'  WHERE tid = $tid";
        $sth1 = $dbconn->prepare($sql) or die $dbconn->errstr;
        $sth1->execute() or die "错误: $sth->errstr";
	close FH;
	
}

#设置任务状态
sub setTaskStatus {

	$rid = shift||"";
	$status = shift||"";
	$scomment = shift||"";

	my_connect();

        $sql = "update c_servertask set status =\'$status\', scomment=\'$scomment\' where rid = $rid";

        $sth = $dbconn->prepare($sql) or die $dbconn->errstr;

        $sth->execute() or die "错误: $sth->errstr";

}

#设置任务进度
sub setTaskProgress {

	$rid = shift||"";
	$progress = shift||0;

	my_connect();

        $sql = "update c_servertask set progress =$progress where rid = $rid";

        $sth = $dbconn->prepare($sql) or die $dbconn->errstr;

        $sth->execute() or die "错误:$sth->errstr";

}

1;
